package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResult;
import com.itheima.common.R;
import com.itheima.common.RoleResult;
import com.itheima.dao.SysRoleDao;
import com.itheima.dao.SysRoleMenuDao;
import com.itheima.pojo.SysRole;
import com.itheima.pojo.SysRoleMenu;
import com.itheima.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SysRoleMenuDao sysRoleMenuMapper;

    @Override
    public R findList(Integer pageNum, Integer pageSize, SysRole sysRole) {
        IPage<SysRole> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if (sysRole.getRoleName() != null) {
            wrapper.like(SysRole::getRoleName, sysRole.getRoleName());
        }
        if (sysRole.getRoleKey() != null) {
            wrapper.like(SysRole::getRoleKey, sysRole.getRoleKey());
        }
        if (sysRole.getStatus() != null) {
            wrapper.eq(SysRole::getStatus, sysRole.getStatus());
        }
        if (sysRole.getCreateBeginTime() != null && sysRole.getCreateEndTime() != null) {
            wrapper.ge(SysRole::getCreateTime, sysRole.getCreateBeginTime());
            wrapper.le(SysRole::getCreateTime, sysRole.getCreateEndTime());
        }
        IPage<SysRole> sysRoleIPage = sysRoleMapper.selectPage(page, wrapper);
        PageResult result = new PageResult();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setTotal(sysRoleIPage.getTotal());
        result.setPages(sysRoleIPage.getPages());
        result.setResults(sysRoleIPage.getRecords());
        return new R(1, "查询成功", result);
    }

    @Override
    public R addRole(SysRole sysRole) {
        //补全数据
        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute("userName");
        sysRole.setCreateBy(userName);
        sysRole.setCreateTime(new Date());
        sysRole.setDeptCheckStrictly(1);
        sysRole.setMenuCheckStrictly(1);
        sysRole.setDataScope("4");
        sysRole.setDelFlag("2");
        sysRoleMapper.insert(sysRole);
        //添加角色所包含的菜单id
        List<Long> checkedKeys = sysRole.getCheckedKeys();
        for (Long checkedKey : checkedKeys) {
            Long roleId = sysRole.getRoleId();
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(checkedKey);
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
        return new R(1, "添加成功",null);
    }

    @Override
    public RoleResult findById(Long roleId) {
        //根据id查找数据
        SysRole sysRole = sysRoleMapper.selectById(roleId);
        //根据id查找menuId集合
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(wrapper);
        List<Long> checkedKeys = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
            checkedKeys.add(sysRoleMenu.getMenuId());
        }
        RoleResult result = new RoleResult<>();
        result.setCheckedKeys(checkedKeys);
        result.setData(sysRole);
        result.setCode(1);
        result.setMsg("操作成功");
        return result;
    }

    @Override
    public R updateRole(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
        //先删除menuId
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,sysRole.getRoleId());
        sysRoleMenuMapper.delete(wrapper);
        //添加新的menuId
        List<Long> checkedKeys = sysRole.getCheckedKeys();
        for (Long checkedKey : checkedKeys) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(checkedKey);
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
        return new R(1,"修改成功",null);
    }

    @Override
    public R deleteById(Long roleId) {
        //删除menuId
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,roleId);
        sysRoleMenuMapper.delete(wrapper);
        //删除角色数据
        sysRoleMapper.deleteById(roleId);
        return new R(1,"删除成功",null);
    }
}
